﻿// -----------------------------------------------------------------------
// <copyright file="BenhNhanServices.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System.Windows.Forms;

namespace ASS.BUS.Services
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ASS.BUS.DTO;
    using ASS.DAO;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class BenhNhanServices
    {
        public bool Add(BenhNhan benhNhan)
        {
            var addSuccsess = false;
            using (var db = new DataContext())
            {
                try
                {
                    if (db.BenhNhans != null) db.BenhNhans.Add(benhNhan);
                    db.SaveChanges();
                    addSuccsess = true;
                }
                catch (Exception ex)
                {
                    addSuccsess = false;
                    MessageBox.Show(ex.Message);
                }
            }
            return addSuccsess;
        }

        public BenhNhan GetBenhNhan(int id)
        {
            using (var db = new DataContext())
            {
                var item = db.BenhNhans.Single(x => x.MaBn == id);
                return item;
            }
        }

        public List<BenhNhan> GetBenhNhans()
        {
            using (var db = new DataContext())
            {
                var items = db.BenhNhans.ToList();

                return items;
            }
        }

        public bool EditBenhNhan(BenhNhan benhNhan)
        {
            using (var db = new DataContext())
            {
                var item = db.BenhNhans.SingleOrDefault(x => x.MaBn == benhNhan.MaBn);

                if (item == null) return false;
                item.Ten = benhNhan.Ten;
                item.DiaChi = benhNhan.DiaChi;
                item.NgaySinh = benhNhan.NgaySinh;
                item.GioiTinh = benhNhan.GioiTinh;
                item.MucDo = benhNhan.MucDo;
                db.SaveChanges();
                return true;
            }
        }

        public bool DeleteBenhNhan(int id)
        {
            using (DataContext db = new DataContext())
            {
                var item = db.BenhNhans.SingleOrDefault(x => x.MaBn == id);

                if (item == null) return false;

                db.BenhNhans.Remove(item);
                db.SaveChanges();                
                return true;
            }
        }
    }
}
